//
//  THScrollChooseView.m
//  THChooseTool


#define THScreenW [UIScreen mainScreen].bounds.size.width

#define THScreenH [UIScreen mainScreen].bounds.size.height

//#define THWParam [UIScreen mainScreen].bounds.size.width/375.0f
//
//#define THfloat(a) a*THWParam

#import "THScrollChooseView.h"


@interface THScrollChooseView()<UIPickerViewDataSource, UIPickerViewDelegate>


@property (strong, nonatomic) UIPickerView *pickerView;

@property (strong, nonatomic) UIView *bottomView;

/**
 取消按钮
 */
@property (strong, nonatomic) UIButton *cancelButton;

/**
 确定按钮
 */
@property (strong, nonatomic) UIButton *confirmButton;

/**
 选中数据是第几条
 */
@property (assign, nonatomic) NSInteger selectedValue;

/**
 数组
 */
@property (strong, nonatomic) NSArray *questionArray;
/**
 默认的值
 */
@property (copy, nonatomic) NSString *defaultDesc;

@end

@implementation THScrollChooseView


static NSInteger recordRowOfQuestion;


- (instancetype)initWithQuestionArray:(NSArray *)questionArray withDefaultDesc:(NSString *)defaultDesc with:(NSString *)title{
    
    if (self = [super init]) {
        
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
        
        self.frame = CGRectMake(0, 0, THScreenW, THScreenH);
        
        UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-230, CGRectGetWidth(self.frame), 230)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [self addSubview:whiteView];
        
        //按钮所在区域
        UIView *viewBg = [[UIView alloc] initWithFrame:CGRectMake(0,0,THScreenW,45)];
        [viewBg setBackgroundColor:[UIColor whiteColor]];
        [whiteView addSubview:viewBg];
        
        //创建取消 确定按钮
        UIButton *cannel = [UIButton buttonWithType:UIButtonTypeCustom];
        cannel.frame = CGRectMake(20, 0, 50,45);
        [cannel setTitle:@"取消" forState:0];
        cannel.titleLabel.font = [UIFont systemFontOfSize:16];
        [cannel setTitleColor:[UIColor cz_colorWithRed:166 green:166 blue:166] forState:UIControlStateNormal];
        cannel.tag = 1;
        [cannel addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [viewBg addSubview:cannel];
        
        UIButton *confirm = [UIButton buttonWithType:UIButtonTypeCustom];
        confirm.frame = CGRectMake(THScreenW - 70, 0, 50, 45);
        [confirm setTitle:@"确定" forState:0];
        confirm.titleLabel.font = [UIFont systemFontOfSize:16];
        [confirm setTitleColor:[UIColor cz_colorWithRed:68 green:68 blue:68] forState:UIControlStateNormal];
        confirm.tag = 2;
        [confirm addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [viewBg addSubview:confirm];
        
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.text = title;
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.textColor = [UIColor cz_colorWithRed:52 green:52 blue:52];
        [viewBg addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(whiteView);
            make.centerY.equalTo(confirm);
        }];
        
        UILabel *lineLabel = [[UILabel alloc]init];
        lineLabel.backgroundColor = [UIColor cz_colorWithRed:216 green:216 blue:216];
        [viewBg addSubview:lineLabel];
        [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@1);
            make.bottom.mas_equalTo(viewBg.mas_bottom);
        }];
        
        self.questionArray = questionArray;
        self.defaultDesc = defaultDesc;
        [whiteView addSubview:self.pickerView];
        
    }
    
    return self;
}


#pragma mark - action

- (void)cancelButtonAction:(UIButton *)button {
    
    [self dismissView];
    
}

- (void)confirmButtonAction:(UIButton *)button {
    
    self.confirmBlock(self.selectedValue);
    
    [self dismissView];
}

- (void)showView{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
        
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)dismissView{
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissView];
}


#pragma mark - pickerView 代理方法

// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

// 行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.questionArray.count;
}

-(CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component {
    return 35;
}

// 显示什么
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return self.questionArray[row];
}

// 选中时
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    self.selectedValue = row;
    
    UILabel *titleLabel = (UILabel *)[self.pickerView viewForRow:row forComponent:component];
    titleLabel.textColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
    //    for(UIView *singleLine in pickerView.subviews){
    //        if (singleLine.frame.size.height < 1){
    //            singleLine.backgroundColor = [UIColor cz_colorWithRed:215 green:215 blue:215];
    //
    //        }
    //    }
        //设置文字的属性
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 30)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = self.questionArray[row];
    //    NSLog(@"selectedValue:%ld,row:%ld,component:%ld",self.selectedValue,row,component);
    titleLabel.textColor = [UIColor cz_colorWithRed:60 green:60 blue:60];
    
    //    for (NSString *str in self.questionArray) {
    //        NSLog(@"str::::::%@,%@",str,self.defaultDesc);
    //        if ([str isEqualToString:self.defaultDesc]) {
    //            titleLabel.textColor = [UIColor cz_colorWithRed:238 green:81 blue:39];
    //        }else{
    //            titleLabel.textColor = [UIColor cz_colorWithRed:60 green:60 blue:60];
    //        }
    //    }
    //    [self.picker picke]
    
        return titleLabel;
    
}

- (UIPickerView *)pickerView {
    
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 45, THScreenW, 230 - 45)];
        _pickerView.backgroundColor = [UIColor clearColor];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        
        // _pickerView 初始化显示的值
        if (self.defaultDesc.length == 0) {
            //无默认值时显示第一个
            [_pickerView selectRow:0 inComponent:0 animated:YES];
            
        }else {
            //有默认值时显示默认值
            recordRowOfQuestion = [self rowOfQuestionWithName:self.defaultDesc];
            [_pickerView selectRow:recordRowOfQuestion inComponent:0 animated:YES];
            self.selectedValue = recordRowOfQuestion;
            
        }
    }
    return _pickerView;
}


- (NSInteger)rowOfQuestionWithName:(NSString *)questionName{
    
    NSInteger row = 0;
    for (NSString *str in self.questionArray) {
        if ([str containsString:self.defaultDesc]) {
            return row;
        }
        row++;
    }
    return row;
}




@end
